package com.avcdata.national.utils;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.Toast;

import com.avcdata.aoscustommade.main.MyApplication;


/**
 * NewAOSApp
 * com.avcdata.national.utils
 * Created by lic on 2017/8/23.
 * CSDN http://blog.csdn.net/lucasey
 *
 * 重新封装fragment
 */

public abstract class MyBaseFragment extends Fragment implements View.OnClickListener  {
    protected Context context;
    protected Activity baseFragmentActivity;
    private View view;
    private ProgressDialog progressDialog;
    public Bundle savedInstanceState;

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        context = activity.getApplicationContext();
        this.baseFragmentActivity = activity;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        view = onLayoutView(inflater, container, savedInstanceState);
        if (view != null)
            initView(view, savedInstanceState);
        return view;
    }

    /**
     * 初始化布局
     */
    protected abstract View onLayoutView(LayoutInflater inflater,
                                         ViewGroup container, Bundle savedInstanceState);

    /**
     * 页面初始化
     *
     * @param container
     * @param savedInstanceState
     */
    protected abstract void initView(View container, Bundle savedInstanceState);

    protected abstract void onclickEvent(View view);

    /**
     * 短时间显示Toast
     *
     * @param info 显示的内容
     */
    public void showToast(String info) {
        Toast.makeText(MyApplication.softApplication, info, Toast.LENGTH_SHORT).show();
    }

    /**
     * 长时间显示Toast
     *
     * @param info 显示的内容
     */
    public void showToastLong(String info) {
        Toast.makeText(MyApplication.softApplication, info, Toast.LENGTH_LONG).show();
    }

    /**
     * 短时间显示Toast
     */
    public void showToast(int resId) {
        Toast.makeText(MyApplication.softApplication, resId, Toast.LENGTH_SHORT).show();
    }

    /**
     * 长时间显示Toast
     */
    public void showToastLong(int resId) {
        Toast.makeText(MyApplication.softApplication, resId, Toast.LENGTH_LONG).show();
    }

    /**
     * 得到屏幕宽度
     *
     * @return 宽度
     */
    public int getScreenWidth() {
        DisplayMetrics dm = new DisplayMetrics();
        baseFragmentActivity.getWindowManager().getDefaultDisplay().getMetrics(dm);
        int screenWidth = dm.widthPixels;
        return screenWidth;
    }

    /**
     * 得到屏幕高度
     *
     * @return 高度
     */
    public int getScreenHeight() {
        DisplayMetrics dm = new DisplayMetrics();
        baseFragmentActivity.getWindowManager().getDefaultDisplay().getMetrics(dm);
        int screenHeight = dm.heightPixels;
        return screenHeight;
    }

    @Override
    public void onClick(View v) {
        onclickEvent(v);
    }


    /**
     * 显示正在加载的进度条
     */
    public void showProgressDialog() {
        if (progressDialog != null && progressDialog.isShowing()) {
            progressDialog.dismiss();
            progressDialog = null;
        }
        progressDialog = new ProgressDialog(getActivity());
        progressDialog.setCanceledOnTouchOutside(false);
        try {
            progressDialog.show();
        } catch (WindowManager.BadTokenException exception) {
            exception.printStackTrace();
        }
    }

    public void showProgressDialog(String msg) {
        if (progressDialog != null && progressDialog.isShowing()) {
            progressDialog.dismiss();
            progressDialog = null;
        }
        progressDialog = new ProgressDialog(getActivity());
        progressDialog.setMessage(msg);
        progressDialog.setCanceledOnTouchOutside(false);
        try {
            progressDialog.show();
        } catch (WindowManager.BadTokenException exception) {
            exception.printStackTrace();
        }
    }

    /***
     * 创建一个ProgressDialog
     *
     * @param msg 显示的内容
     * @return ProgressDialog
     */
    public ProgressDialog createProgressDialog(String msg) {
        ProgressDialog progressDialog = new ProgressDialog(getActivity());
        progressDialog.setMessage(msg);
        progressDialog.setCanceledOnTouchOutside(false);
        return progressDialog;
    }

    /**
     * 隐藏正在加载的进度条
     */
    public void dismissProgressDialog() {
        if (null != progressDialog && progressDialog.isShowing() == true) {
            progressDialog.dismiss();
            progressDialog = null;
        }
    }

    /**
     * 重新计算ListView的高度，解决ScrollView和ListView两个View都有滚动的效果，在嵌套使用时起冲突的问题
     *
     * @param listView
     */
    public void setListViewHeight(ListView listView) {
        // 获取ListView对应的Adapter
        ListAdapter listAdapter = listView.getAdapter();
        if (listAdapter == null) {
            return;
        }
        int totalHeight = 0;
        for (int i = 0, len = listAdapter.getCount(); i < len; i++) { // listAdapter.getCount()返回数据项的数目
            View listItem = listAdapter.getView(i, null, listView);
            listItem.measure(0, 0); // 计算子项View 的宽高
            totalHeight += listItem.getMeasuredHeight(); // 统计所有子项的总高度
        }
        /*
         * View listItem = listAdapter.getView(0, null, listView);
		 * listItem.measure(0, 0); // 计算子项View 的宽高 totalHeight =
		 * (listItem.getMeasuredHeight())*(listAdapter.getCount());
		 */
        ViewGroup.LayoutParams params = listView.getLayoutParams();
        params.height = totalHeight
                + (listView.getDividerHeight() * (listAdapter.getCount() - 1));
        listView.setLayoutParams(params);
    }
}